<!--
 * @Description: 病虫害处理记录
-->
<template>
  <BaseMainSlot :show-btn-box="false">
    <template v-slot:search-box>
      <SearchForm :formConfig="formConfig" :formData="searchlist" @search="search"></SearchForm>
    </template>
    <template v-slot:table-box>
      <Table @sortChange="changeTableSort" :propList="tableColumn" v-loading="loading"
        :listData="tableData" :showSelectColumn="false" :toolTip="false">
        <template v-slot:operation="{ row }">
          <template v-if="!row.index">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="lookInfo(row)"
            >
              <i class="el-icon-view"></i> 点击查看
            </el-button>
          </template>
        </template>
        <template v-slot:facilities="{ row, item }">
          <template v-if="!row.index">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="checkFacility(row, item.prop)"
            >
              <i class="el-icon-view"></i> 查看
            </el-button>
          </template>
        </template>
        <template v-slot:img="{ row }">
          <template v-if="row.alarmBatchNo">
            <div v-if="row.pictureId" class="viewImgContainer">
              <div class="viewImg" @click="getpicmessage(row.pictureId)">
                <img
                  class="viewImg"
                  v-show="row.viewImg"
                  src="@/assets/unifiedPicture/viewImg1.png"
                />
              </div>
            </div>
            <el-button v-else type="text" disabled
              >{{ row.index }}暂无图片</el-button
            >
          </template>
          <!-- <template v-if="!row.index">
            <el-button
              v-if="row.pictureId"
              type="success"
              @click="getpicmessage(row.pictureId)"
            >点击查看</el-button>
            <span v-else>暂无图片</span>
          </template> -->
        </template>
      </Table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog title="设施列表" :visible="facilityDialogVisible"
        class="dialogRec" :before-close="()=>{facilityDialogVisible = false}">
        <div class="facility-list">
          <div v-for="(item,index) in batchFacilityMapperList" :key="index" class="facility-item">
            <div class="info-line">
              <div class="name">设施编号：</div><div class="value">{{ item.facilityNo }}</div>
            </div>
            <div class="info-line">
              <div class="name">设施名称：</div><div class="value">{{ item.facilityName }}</div>
            </div>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="facilityDialogVisible = false">
            关 闭
          </el-button>
        </div>
      </el-dialog>
      <!-- 查看编号 -->
      <el-dialog
        title="单个编号"
        :visible.sync="showIdNameList"
        width="30%"
        class="dialogRec"
        @close="showIdNameList = false"
      >
        <div style="display: flex">
          <div v-for="(item, index) in singleNoList" :key="index">
            <div
              v-if="!!activeSingleNoList"
              class="IdNameList"
              :class="
                activeSingleNoList.includes(item)
                  ? 'IdNameList activeId'
                  : 'IdNameList'
              "
            >
              {{ item }}
            </div>
            <div v-else class="IdNameList">{{ item }}</div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="showIdNameList = false"
            >关 闭</el-button
          >
        </span>
      </el-dialog>
      <el-dialog
        id="tableColumnDialog"
        title="配置列"
        :visible.sync="dialogTableColumnVisible"
      >
        <tableColumn
          v-if="dialogTableColumnVisible"
          ref="tableColumn"
          :tableColumn="allTableColumn"
          @cancelColumn="cancelColumn"
        />
        <div slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="dialogTableColumnVisible = false">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="saveColumn"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
      <el-dialog
        title="图片"
        :visible.sync="showpicview"
        width="30%"
        v-loading="imgloading"
      >
      <div
          style="
            width: 100%;
            height: 100%;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
          "
        >
        <img
          v-for="item,index in pictviewurl"
          :key="index"
          style="max-width: 100%;"
          :src="`data:image/png;base64,${item.pictureData}`"
        />
        </div>
        <span
          slot="footer"
          class="dialog-footer"
        >
          <el-button
            class="zh-button-one"
            @click="showpicview = false"
          >取 消</el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="showpicview = false"
          >确 定</el-button>
        </span>
      </el-dialog>
      <!-- 查看详情 -->
      <el-dialog
        title="处理信息"
        :visible.sync="showInfo"
      >
        <div style="width: 100%; height: 100%">
          <div
            class="info"
            v-for="(item, index) in inputsRecordBOList"
            :key="index"
          >
            <div class="line">
              <div class="right">
                <div class="name">处理人：</div>
                <div class="value">{{ item.personName || undefined }}</div>
              </div>
              <div class="left">
                <div class="name">处理日期：</div>
                <div class="value">{{ item.assetDate || undefined }}</div>
              </div>
            </div>
            <div class="line">
              <div class="right">
                <div class="name">处理图片：</div>
                <div class="value" v-if="item.url"><img :src="item.url" alt="" style="height: 100%;"></div>
              </div>
              <div class="left">
                <div class="name">备注：</div>
                <div class="value">{{ item.remark || '' }}</div>
              </div>
            </div>
            <div class="line">
              <div class="right">
                <div class="name">治疗药物：</div>
                <div class="value">{{ item.materialDTO ? item.materialDTO.materialName : '' }}</div>
              </div>
              <div class="left">
                <div class="name">投入量：</div>
                <div class="value">{{ item.assetQuantity || '' }}</div>
              </div>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="showInfo = false"
            >取 消</el-button
          >
          <el-button
            class="zh-button-two"
            type="primary"
            @click="showInfo = false"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot.vue";
import SearchForm from '@/components/searchForm';
import Table from '@/components/table/table.vue';
import Pagination from "@/components/Pagination";
import pestInfoApi from "@/api/basic-info-manage/pest-info/index";
// TODO:
import API from "@/api/digital-production/health-management/pest-follow-up/index";
import userApi from "@/api/user/index";

import { mymixin } from "@/utils/mixin";
import { farmingTypeTitle } from "@/utils/validate";

export default {
  name: "ProductionBatchQuery",
  components: {
    SearchForm,
    Table,
    Pagination,
    BaseMainSlot,
  },
  mixins: [mymixin],
  data() {
    var farmingTypeTitles = farmingTypeTitle()
    return {
      batchTypeList: [{ name: "批次管理" }, { name: "单个管理" }],
      tableColumn: [
        { prop: 'alarmBatchName', label: '批次名称', minWidth: '200' },
        { prop: 'alarmBatchNo', label: '批次号', minWidth: '150' },
        { prop: 'facilityNoList', label: '发现设施', slotName: 'facilities', minWidth: '110' },
        { prop: 'IdNameList', label: '患病个体', slotName: 'facilities', minWidth: '110' },
        { prop: 'alarmTypeName', label: farmingTypeTitles+'类别', minWidth: '110' },
        { prop: 'alarmDes', label: farmingTypeTitles+'名称', minWidth: '110' },
        { prop: 'pictureId', label: '图片', slotName: 'img', minWidth: '110' },
        { prop: 'disposeDate', label: '发现日期', minWidth: '240' },
        { prop: 'operation', label: '处理详情', slotName: 'operation', minWidth: '110' },
      ],
      formConfig: [
        {
          label: '批次名称',
          prop: 'alarmBatchNo',
          type: 'select',
          options: [],
          optionKey: { label: 'batchName', value: 'batchNo' },
        }, {
          label: '批次号',
          prop: 'alarmBatchNo',
          type: 'select',
          options: [],
          optionKey: { label: "batchNo", value: "batchNo" },
        }, {
          label: '病虫害类别',
          prop: 'alarmTypeName',
          type: 'select',
          options: [
            { name: '病害' },
            { name: '虫害' },
          ],
          optionKey: { label: 'name', value: 'name' }
        }, {
          label: '记录日期',
          prop: 'date',
          type: 'daterange',
        },
      ],
      searchlist: {},
      farmingTypeTitles: farmingTypeTitle(),
      // 异常原因
      alarmTypeList: [
        { name: "病害", value: 0 },
        { name: "虫害", value: 1 }
      ],
      alarmTypeLists: [{ name: "病害", value: 0 }],
      // 当前表格选择的数组
      nowSelectList: [],
      batchFacilityMapperList: [],
      facilityDialogVisible: false,
      showpicview: false,
      imgloading: false,
      pictviewurl: [],
      showIdNameList: false,
      singleNoList: [],
      showInfo: false,
      inputsRecordBOList: false,
    };
  },
  watch: {},
  created() {
    this.setOptions(this.formConfig, 'alarmTypeName', this.farmingTypeTitles == '病害' ? this.alarmTypeLists : this.alarmTypeList)
    this.search();
  },
  mounted() {
    this.getselectdatalist();
  },
  methods: {
    async checkFacility(row, prop) {
      if (prop == 'facilityNoList') {
        let list = [];
        if (row.facilityNoList) {
          row.facilityNoList.map((el) => {
            let index = this.facilityList.findIndex((ele) => {
              return el === ele.facilityNo;
            });
            list.push({
              facilityNo: el,
              facilityName: this.facilityList[index].facilityName,
            });
          });
        }
        this.batchFacilityMapperList = list;
        this.facilityDialogVisible = true;
      } else {
        this.showIdNameList = true;
        let res = await API.queryIdList(row.alarmBatchNo);
        this.singleNoList = res.data.singleNoList;
        this.activeSingleNoList = [];
        row.singleMappers.forEach((el) => {
          this.activeSingleNoList.push(el.singleNo);
        });
      }
    },
    // 查看详情
    lookInfo(row) {
      this.inputsRecordBOList = row.inputsRecordBOList;
      this.inputsRecordBOList && this.inputsRecordBOList.forEach((el, i) => {
        API.queryPictureNo(el.pictureNo).then(async (res) => {
          // el.url = "data:image/png;base64," + res.data.pictureData;
          this.$set(this.inputsRecordBOList[i], 'url', "data:image/png;base64," + res.data.pictureData)
        });
      });
      this.showInfo = true;
    },
    // 获取下拉框数据
    async getselectdatalist() {
      // 获取所有批次
      API.getAllBatchList({ batchType: "BATCH",simpleData: true }).then((res) => {
        this.batchList = res.data;
        this.formConfig[0].options = res.data;
        this.formConfig[1].options = res.data;
      });
      userApi.getfacilityAll({
        organizationList: sessionStorage
          .getItem("organizationList")
          .split(","),
        farmingTypeNo: sessionStorage.getItem("farmingTypeNo"),
      })
      .then((res) => {
        this.facilityList = res.data;
      });
    },
    // 获取图片详情
    getpicmessage(id) {
      this.showpicview = true;
      this.imgloading = true;
      pestInfoApi.queryPictureList(id).then(res => {
        this.pictviewurl = res.data
        this.imgloading = false;
      });
    },
    // 表格查询
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
        entity: {
          // ...this.searchlist,
          organizationNo: sessionStorage.getItem("organizationNo"),
          alarmBatchNo: this.searchlist.alarmBatchNo || undefined,
          alarmTypeName: this.searchlist.alarmTypeName || undefined,
          disposeAction: 1,
          startTime: this.searchlist.date && this.dateToFormattwo(this.searchlist.date[0]),
          endTime: this.searchlist.date && this.dateToFormattwo(this.searchlist.date[1]),
        }
      };
      delete params.entity.date
      this.entity = params.entity
      API.getList(params).then(res => {
        this.loading = false;
        if (res.code === 1) {
          // this.$message.success('查询成功')
        } else {
          this.$message.error("查询失败");
        }
        let data = res.data.content;
        this.tableData = data;
        // 赋值总条数
        this.totalSize = res.data.totalElements;
        this.addblankmet();
        this.resetActionBar();
      });
      // 判断搜索输入框数据
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    handleEdit(row) {
      if (row.id) {
        this.formData = row;
        this.editActive = true;
        this.saveActive = false;
        this.cancelActive = true;
        this.canInput = false;
      }
    },
    handleSelectionChange(list) {
      this.nowSelectList = [];
      this.nowSelectList = list.map(item => item.batchNo);
      if (this.nowSelectList.length === 1) this.delActive = true;
      else this.delActive = false;
    },
  }
};
</script>

<style scoped lang="scss">
.facility-list {
  display: flex;
  justify-content: flex-start;
  flex-direction: row;
  flex-wrap: wrap;
  .facility-item {
    width: 400px;
    max-width: 30%;
    min-width: 30%;
    margin-right: 3%;
    margin-bottom: 10px;
    // height: 80px;
    border-radius: 5px;
    // border: 1px solid #ccc;
    box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.15);
    padding: 10px 20px;

    .info-line {
      display: flex;
      justify-content: flex-start;
      align-items: flex-start;
      font-size: 14px;
      &:not(:last-child) {
        margin-bottom: 10px;
      }
      .name {
        text-align: left;
        width: 80px;
        flex-shrink: 0;
      }
      .value {
        width: 100%;
        text-align: left;
        font-weight: normal;
      }
    }
  }
}
::v-deep .tab-select {
  width: 100%;
  .butt-list-sty {
    position: absolute;
    left: 5px;
    top: 7px;
  }
}
// 查看编号
.IdNameList {
  width: 120px;
  height: 33px;
  margin-right: 10px;
  color: #000;
  text-align: center;
  line-height: 33px;
  border: 1px solid #006e91;
  &.activeId {
    background-color: #006e91;
    color: #fff;
  }
}
// .info {
//   margin-bottom: 10px;
//   border-bottom: 1px dashed #ccc;
//   &:last-child {
//     border-bottom: none;
//   }
//   .line {
//     display: flex;
//     margin-bottom: 15px;
//     padding-bottom: 15px;
//     padding-left: 30px;
//     font-size: 16px;
//     // align-items: center;
//     // justify-content: space-between;
//     &>div {
//       display: flex;
//       width: 50%;
//       .name {
//         min-width: 100px;
//       }
//     }
//   }
// }
.info {
  margin-bottom: 10px;
  border-bottom: 1px dashed #ccc;
  .line {
    display: flex;
    justify-content: flex-start;
    margin-bottom: 20px;
    font-size: 16px;
    .right {
      width: 50%;
      display: flex;
      align-items: center;
      .value {
        max-width: 70%;
        img {
          width: 100%;
        }
      }
    }
    .left {
      flex: 1;
      display: flex;
      align-items: center;
    }
  }
}

.viewImgContainer {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  .viewImg {
    width: 80px;
    height: 32px;
    cursor: pointer;
    background: url("~@/assets/unifiedPicture/viewImg-d.png") no-repeat 100%
      100%;
    &:hover {
      background: url("~@/assets/unifiedPicture/viewImg.png") no-repeat 100%
        100%;
    }
  }
}
</style>
